package com.sdgakj.repository;

import com.sdgakj.entity.Feed;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

public class FeedDaoImpl {

    @PersistenceContext
    private EntityManager em;

    /**
     * 时间倒叙帖子列表
     * 
     * @param customerId
     * @param tagId
     * @param sinceId
     * @param maxId
     * @param pageSize
     * @return
     */
    public List<Feed> findTimeline(Long customerId, Long tagId, Integer isTop, Long sinceId, Long maxId, Integer pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT \n");
        sql.append(" t1.* \n");
        sql.append("FROM \n");
        sql.append("  f_feed t1 \n");
        sql.append("WHERE 1 = 1 \n");
        sql.append("  AND t1.deltag = '0' \n");
        if(customerId != null) {
            sql.append("  AND t1.customer_id = :customerId \n");
        }
        if(tagId != null) {
        	sql.append("  AND t1.tag_id = :tagId \n");
        }
        if(isTop != null) {
            sql.append("  AND t1.is_top = :isTop \n");
        }
        if(sinceId != null) {
            sql.append("  AND t1.id > :sinceId \n");
        } else if(maxId != null) {
            sql.append("  AND t1.id < :maxId \n");
        }
        sql.append("ORDER BY t1.id DESC \n");
        sql.append("LIMIT 0, :rows \n");

        Query q = em.createNativeQuery(sql.toString(), Feed.class);
        if(customerId != null) {
            q.setParameter("customerId", customerId);
        }
        if(tagId != null) {
        	q.setParameter("tagId", tagId);
        }
        if(isTop != null) {
            q.setParameter("isTop", isTop);
        }
        if(sinceId != null) {
            q.setParameter("sinceId", sinceId);
        } else if(maxId != null) {
            q.setParameter("maxId", maxId);
        }
        q.setParameter("rows", pageSize);
//      List<Feed> result = q.getResultList();
        return q.getResultList();
//      sql = new StringBuffer();
//        sql.append("SELECT \n");
//        sql.append("  COUNT(1) \n");
//        sql.append("FROM \n");
//      sql.append("  sht_feed t1 \n");
//      sql.append("  AND t1.deltag = '0' \n");
//      if(customerId != null) {
//          sql.append("  AND t1.customer_id = :customerId \n");
//      }
//      if(tagId != null) {
//          sql.append("  AND t1.tag_id = :tagId \n");
//      }
//      if(sinceId != null) {
//          sql.append("  AND t1.id > :sinceId \n");
//      } else if(maxId != null) {
//          sql.append("  AND t1.id < :maxId \n");
//      }
//        q = em.createNativeQuery(sql.toString());
//        long total = ((BigInteger) q.getSingleResult()).longValue();
//
//        Pageable pageable = new PageRequest(sinceId.intValue() / pageSize, pageSize);
//        List<Feed> result = new PageImpl<Feed>(resutcontent, pageable, total);
//      return result;
    }

    public List<Feed> getFeedLikeTitle(String content){
        StringBuffer sql = new StringBuffer();
        sql.append("select * from f_feed t where 1=1 ");
        if(content!=null && content.length()>0){
            sql.append(" and content like '%"+content);
            sql.append("%'");
        }
        Query q = em.createNativeQuery(sql.toString(), Feed.class);
        return q.getResultList();
    }


}
